<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/27
 * Time: 14:54
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Advert;
use Log;

class AdvertController extends Controller
{

    public function save(Request $request, Advert $advert)
    {
        $advert->name = $request->input('name');
        $advert->url = $request->input('url');
        $advert->save();
        return $this->success();
    }

    public function update(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id <= 0) {
            return $this->error();
        }
        $advert = Advert::find($id);
        if(!$advert){
            return $this->error();
        }

        $advert->name = $request->input('name');
        $advert->url = $request->input('url');
        $advert->save();
        return $this->success();
    }

    public function lists(Request $request, Advert $advert)
    {
        $pageSize = $request->get('pageSize',10);
        $list = $advert::orderBy('id','desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function mobileLists(Request $request, Advert $advert)
    {
        $pageSize = $request->get('pageSize',10);
        $list = $advert::orderBy('id','desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function delete(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id <= 0) {
            return $this->error();
        }
        Advert::destroy($id);
        return $this->success();
    }




}